package com.freecloud.rpc.demo1.client;

import com.freecloud.rpc.demo1.service.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author: maomao
 * @Date: 2021/8/29 3:04 下午
 */
public class RemoteInvocationHandler implements InvocationHandler {

    private String host;
    private int port;

    public RemoteInvocationHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //远程连接
        RpcNetTransport rpcNetTransport = new RpcNetTransport(host, port);
        //封装调用方法
        RpcRequest request = new RpcRequest();
        request.setArgs(args);
        request.setClassName(method.getDeclaringClass().getName());
        request.setTypes(method.getParameterTypes());
        request.setMethodName(method.getName());
        //远程调用
        return rpcNetTransport.send(request);
    }
}
